package dbExec

import (
	greatdb "db2s/Gr"
	mysql "db2s/My"
	oracle "db2s/Or"
	"db2s/global"
	"reflect"
	"time"
)

type (
	Partition struct {
		DBname         string
		Schema         string        `json:"schema"`
		Table          string        `json:"table"`
		SqlExecTimeOut time.Duration `json:"sqlExecTimeout"`
	}
)
type TPartitionEr interface {
	PartitionType(s global.PartitionInput) (p global.PartitionType, err error)
	SubPartitionType(s global.PartitionInput) (p global.SubPartitionType, err error)
	PartitionColumn(s global.PartitionInput) (p []global.PartitionColumn, err error)
	SubPartitionColumn(s global.PartitionInput) (p []global.SubPartitionColumn, err error)
	PartitionName(s global.PartitionInput) (p []global.PartitionName, err error)
	SubPartitionName(s global.PartitionInput) (p []global.SubPartitionName, err error)
	PartitionRules(s global.PartitionInput) (p []global.PartitionRules, err error)
	SubPartitionRules(s global.PartitionInput) (p []global.SubPartitionRules, err error)
	Configuration(s global.PartitionInput) (tpc global.PartitionInfo, err error)
	Sum(s global.PartitionInput) (global.PartitionSummary, error)
}

var partitionAPI = map[string]any{
	"mysql":   &mysql.Partition{},
	"oracle":  &oracle.Partition{},
	"cluster": &greatdb.Partition{},
}

func NewPartitionObject(ff Partition) (t TPartitionEr) {
	mu.Lock()
	defer func() {
		mu.Unlock()
	}()
	if v1, ok := dbTypeInterFuncRef[ff.DBname]; ok {
		if v1.Kind() == reflect.Func {
			args := []reflect.Value{
				reflect.ValueOf(ff),
				reflect.ValueOf(partitionAPI[ff.DBname]),
			}
			i := v1.Call(args)
			for ii := 0; ii < len(i); ii++ {
				t = i[ii].Interface().(TPartitionEr)
			}
		}
	}
	return
}
